package com.gsafety.wellsfort.service.knowledge;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gsafety.wellsfort.common.context.LoginContextUtil;
import com.gsafety.wellsfort.common.util.ApiResponse;
import com.gsafety.wellsfort.dao.knowledge.MaterialPrivilegeDAO;
import com.gsafety.wellsfort.dao.knowledge.TeachingMaterialDAO;
import com.gsafety.wellsfort.dao.security.AdminDAO;
import com.gsafety.wellsfort.dao.security.MenuDAO;
import com.gsafety.wellsfort.dao.security.RoleDAO;
import com.gsafety.wellsfort.domain.dto.material.MaterialDTO;
import com.gsafety.wellsfort.domain.dto.material.MaterialQueryDTO;
import com.gsafety.wellsfort.domain.model.knowledge.MaterialPrivilege;
import com.gsafety.wellsfort.domain.model.knowledge.TeachingMaterial;
import com.gsafety.wellsfort.domain.model.security.Admin;
import com.gsafety.wellsfort.domain.model.security.Menu;
import com.gsafety.wellsfort.domain.model.security.Role;
import com.gsafety.wellsfort.domain.vo.MaterialPrivilegeVO;
import com.gsafety.wellsfort.domain.vo.TeachingAidVO;
import com.gsafety.wellsfort.domain.vo.TeachingMaterialVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class MaterialService {
    @Autowired(required = false)
    TeachingMaterialDAO materialDAO;
    @Autowired(required = false)
    MaterialPrivilegeDAO privilegeDAO;
    @Autowired(required = false)
    RoleDAO roleDAO;
    @Autowired(required = false)
    MenuDAO menuDAO;
    @Autowired(required = false)
    AdminDAO adminDAO;
    public ApiResponse<MaterialDTO> saveMaterial(MaterialDTO materialDto){
        ApiResponse apiResponse=null;
        TeachingMaterial material=new TeachingMaterial();
        BeanUtils.copyProperties(materialDto, material);
        List<MaterialPrivilege> privilegeList=materialDto.getMaterialPrivilege();
        Integer materialId=materialDto.getId();
        Admin admin=LoginContextUtil.getAdmin();
        material.setModified(admin.getId());
        material.setUpdateTime(new Date());
        if(materialId!=null){
            privilegeDAO.deleteByMaterialId(materialId);
            materialDAO.updateByPrimaryKeySelective(material);
        }else{
            material.setCreator(admin.getId());
            material.setCreateTime(new Date());
            materialDAO.insert(material);
            materialDto.setId(material.getId());
        }
        for(MaterialPrivilege privilege:privilegeList){
            privilege.setMaterialId(material.getId());
            privilegeDAO.insert(privilege);
        }
        apiResponse=ApiResponse.success(materialDto);
        return apiResponse;
    }
    public ApiResponse deleteByMaterialId(Integer id){
        ApiResponse apiResponse=null;
        try{
            privilegeDAO.deleteByMaterialId(id);
            materialDAO.deleteByPrimaryKey(id);
            apiResponse=ApiResponse.success("教材删除成功");
        }catch (Exception e){
            apiResponse=ApiResponse.error();
            e.printStackTrace();
        }
        return apiResponse;
    }
    public ApiResponse<TeachingMaterialVO> getMaterial(Integer id){
        ApiResponse<TeachingMaterialVO> apiResponse=null;
        try{

            List<MaterialPrivilegeVO> privilegeVOList= new ArrayList<MaterialPrivilegeVO>();
            List<MaterialPrivilege> privilegeList= privilegeDAO.selectByMaterialId(id);
            for(MaterialPrivilege privilege:privilegeList){
                MaterialPrivilegeVO privilegeVO=new MaterialPrivilegeVO();
                BeanUtils.copyProperties(privilege, privilegeVO);
                Role r=roleDAO.selectByPrimaryKey(privilege.getRoleId());
                Menu m=menuDAO.selectByPrimaryKey(privilege.getMenuId());
                if(m!=null)privilegeVO.setMenuPrivilegeName(m.getName());
                if(r!=null)privilegeVO.setRoleName(r.getName());
                privilegeVOList.add(privilegeVO);
            }
            TeachingMaterial material= materialDAO.selectByPrimaryKey(id);
            TeachingMaterialVO materalVo =new TeachingMaterialVO.VOBuilder(material).build();
            if(materalVo.getModified()!=null){
                Admin admin=adminDAO.selectByPrimaryKey(materalVo.getModified());
                if(admin!=null)materalVo.setModifiedName(admin.getName());
            }

            materalVo.setPrivilegeList(privilegeVOList);
            apiResponse=ApiResponse.success(materalVo);
        }catch (Exception e){
            apiResponse=ApiResponse.error();
            e.printStackTrace();
        }
        return apiResponse;
    }
    public ApiResponse<List<TeachingMaterialVO>> findByPageParams(MaterialQueryDTO dtoParams) {
        ApiResponse<List<TeachingMaterialVO>> apiResponse;
        Admin admin=LoginContextUtil.getAdmin();
        if(dtoParams.getResourceScope()!=null&&dtoParams.getResourceScope()!=0){
            if(admin!=null)dtoParams.setCreator(LoginContextUtil.getAdmin().getId());
        }
        Page page= PageHelper.startPage(dtoParams.getPageNo(), dtoParams.getPageSize());
        List<TeachingMaterial> resultList= materialDAO.selectByParam(dtoParams);
        List<TeachingMaterialVO> voList=new ArrayList<>();

        TeachingMaterialVO materalVo;
        for(TeachingMaterial q:resultList){
            ApiResponse<TeachingMaterialVO> response=getMaterial(q.getId());
            if(response.getData()!=null){
                materalVo=response.getData();
                voList.add(materalVo);
            }
        }
        PageInfo pageInfo= new PageInfo(page);
        pageInfo.setList(resultList);
        apiResponse=ApiResponse.success(voList,(int)pageInfo.getTotal());
        return apiResponse;
    }
}
